package com.teach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbIntegrate;
import com.teach.entity.TbPrize;
import com.teach.mapper.TbIntegrateMapper;
import com.teach.mapper.TbPrizeMapper;
import com.teach.service.LotteryService;
import com.teach.utils.LotteryUtils;
import com.teach.utils.RedisUtils;
/*import org.redisson.Redisson;
import org.redisson.api.RLock;*/
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@Service
public class LotteryServiceImpl implements LotteryService {
    @Resource
    TbIntegrateMapper tbIntegrateMapper;
    @Resource
    TbPrizeMapper tbPrizeMapper;
   /* @Resource
    Redisson redisson;*/
    @Resource
    RedisUtils redisUtils;
    @Resource
    LotteryUtils lotteryUtils;
    @Override
    public ResponseEntity getWard(Integer userId) {
        /*RLock lock = redisson.getLock(userId+"");
        lock.lock();
        //自定义时间加锁
        lock.lock(3, TimeUnit.SECONDS);*/
       // try {
            //根据用户的id查询积分
            TbIntegrate tbIntegrate = tbIntegrateMapper.selectById(userId);
            //判断积分是否足够，足够的话可以抽奖，不够的话不能抽奖
            /*if(tbIntegrate.getIntegrateNum()<100){
                return new ResponseEntity(null,500,"积分不够，不能抽奖",false);
            }*/
            //查询用户抽奖几次了
            /*Integer countUserId = (Integer) redisUtils.hget("lotteryCount","count_"+userId);
            //抽奖返回时让redis自增一，目的是让抽奖人数加一
            redisUtils.hIncream("lottery","count");
            ArrayList<Integer> list = new ArrayList<>();
            if(countUserId!=null){
                if(countUserId>3){
                    //抽奖次数大于3，提示不允许在抽奖了
                    return new ResponseEntity(null,500,"抽奖次数大于3，不允许在抽奖了",false);
                }else{
                    //抽奖次数小于三，根据抽奖的次数来修改积分
                    TbIntegrate integrate = new TbIntegrate();
                    integrate.setIntegrateNum(tbIntegrate.getIntegrateNum()-100);
                    //修改积分
                    tbIntegrateMapper.updateById(integrate);
                }
            }*/
            //判断是第几个用户在抽奖
            /*Integer count  = (Integer) redisUtils.hget("lottery","count");
            //根据当前用户所对应的个数去判断奖品(这个判断是否是大奖)
            TbPrize tbPrize = tbPrizeMapper.selectLuky(count);
            if(tbPrize!=null){
                //不为空是大奖
                return new ResponseEntity(tbPrize.getPrize(),200,"恭喜你抽到大奖",true);
            }else{
                //为空是小奖品(积分)
                Integer integral = lotteryUtils.BackIntegral();
                //根据当前用户的id把抽到的积分加入到所对应的积分表中
                TbIntegrate newTbIntegrate = tbIntegrateMapper.selectById(userId);
                newTbIntegrate.setIntegrateNum(newTbIntegrate.getIntegrateNum()+integral);
                //添加
                tbIntegrateMapper.insert(newTbIntegrate);
                return new ResponseEntity(integral,200,"恭喜你抽中积分",true);
            }*/
        //}finally {
        //    lock.unlock();
        //}
        return null;
    }
}
